/*
 *              Copyright (C) 2011 The MusicMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *            http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.musicmod.plugin.socialize.microblog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

public class PreferencesActivity extends PreferenceActivity {

	EditTextPreference api_base;
	Preference authorize;
	Preference sign_out;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.preferences);

		PreferenceScreen screen = this.getPreferenceScreen();
		api_base = (EditTextPreference) screen
				.findPreference(Constants.KEY_API_BASE);
		authorize = (Preference) screen.findPreference(Constants.KEY_AUTHORIZE);
		sign_out = (Preference) screen.findPreference(Constants.KEY_SIGN_OUT);

		api_base.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			public boolean onPreferenceChange(Preference preference,
					Object value) {
				SharedPrefsUtils.setStringPreference(PreferencesActivity.this,
						Constants.KEY_API_BASE, (String) value);
				return true;
			}

		});

		authorize.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {

				if (!userCredentialsExists()) {
					gotoAuthorization();
				} else {
					confirmAuthorize();
				}
				return true;
			}
		});

		sign_out.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				if (userCredentialsExists()) {
					confirmSignOut();
				}
				return true;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		sign_out.setEnabled(userCredentialsExists());
		if (userCredentialsExists()) {
			authorize.setTitle(R.string.reauthorize);
			authorize.setSummary(R.string.reauthorize_summary);
		}
	}

	private void confirmAuthorize() {
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.reauthorize)
				.setMessage(R.string.reauthorize_message)
				.setPositiveButton(R.string.reauthorize,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface di, int i) {
								gotoAuthorization();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface di, int i) {
							}
						}).show();
	}

	private void confirmSignOut() {
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.sign_out)
				.setMessage(R.string.sign_out_message)
				.setPositiveButton(R.string.sign_out,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface di, int i) {
								SharedPrefsUtils.setStringPreference(
										PreferencesActivity.this,
										Constants.KEY_PASSWORD,
										Constants.DEFAULT_PASSWORD);
								SharedPrefsUtils.setStringPreference(
										PreferencesActivity.this,
										Constants.KEY_PASSWORD,
										Constants.DEFAULT_PASSWORD);
								sign_out.setEnabled(userCredentialsExists());
								if (!userCredentialsExists()) {
									authorize.setTitle(R.string.authorize);
									authorize
											.setSummary(R.string.authorize_summary);
								}
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface di, int i) {
							}
						}).show();
	}

	private void gotoAuthorization() {
		Intent intent = new Intent();
		intent.setClass(this, AuthorizationActivity.class);
		startActivity(intent);
	}

	private boolean userCredentialsExists() {
		if (SharedPrefsUtils.getStringPreference(getApplicationContext(),
				Constants.KEY_USERNAME, Constants.DEFAULT_USERNAME).equals(
				Constants.DEFAULT_USERNAME)
				|| SharedPrefsUtils.getStringPreference(
						getApplicationContext(), Constants.KEY_PASSWORD,
						Constants.DEFAULT_PASSWORD).equals(
						Constants.DEFAULT_PASSWORD)) {
			return false;
		} else {
			return true;
		}
	}
}